package huawei;

import java.util.Arrays;

/**
 * 电脑病毒感染
 */
public class Q96 {
    static int N = 4;
    static int[][] link = {{1,3, 1},{2,1,1},{4,3,5}};
    static int[] dp = new int[N];


    public static void main(String[] args) {

        int start = 2;

        // 需要的时间初始化为最大
        Arrays.fill(dp, Integer.MAX_VALUE);

        dfs(start, 0);

        boolean is = true;
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < N; i++) {
            if (dp[i] == Integer.MAX_VALUE) {
                is = false;
                break;
            } else {
                max = Math.max(max, dp[i]);
            }
        }

        if (!is) {
            System.out.println(-1);
        } else {
            System.out.println(max);
        }
    }

    public static void dfs (int t, int time) {
        if (dp[t - 1] > time) {
            dp[t - 1] = time;
            for(int[] lk : link) {
                if (lk[0] == t) {
                    int next = lk[1];
                    dfs(next, time + lk[2]);
                }
            }
        }
    }
}
